package com.ziJing.multithreading.sync.t_05_ThreadLocal;

/**
 * @program Multithreading
 * @description:
 * @author: zhangbo
 * @create: 2022/05/29 21:25
 */
public class T01_HelloThreadLocal {

    ThreadLocal<String> tl = new ThreadLocal<>();

    public void test() throws Throwable {
        tl.set("hello");
        this.finalize();


        Thread.sleep(10000);
        tl.get();
        tl.remove();
    }

    public void set() {
        ThreadLocal<String> threadLocal = new ThreadLocal<>();
        threadLocal.set("hello");
    }

    public String get() {
        ThreadLocal<String> threadLocal = new ThreadLocal<>();
        return threadLocal.get();
    }


    public static void main(String[] args) throws InterruptedException {
        T01_HelloThreadLocal test1 = new T01_HelloThreadLocal();
        test1.tl.set("hellosa");
        Thread t = new Thread(() -> {
            T01_HelloThreadLocal test = new T01_HelloThreadLocal();
            try {
                Thread.sleep(1000);
                test.test();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        });
        t.start();
        t.join();

        System.out.println("test1.get(): " + test1.tl.get());
    }
}
